/*
 *  $TEAM_NAME Space Trader
 */
package views;

import models.Marketplace;
import models.TradeGood;
import models.Player;
import models.Ship;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import javax.swing.JLabel;

import javax.swing.JTextField;

import javax.swing.table.DefaultTableModel;

/**
 * Market Panel
 * @author twb
 * @version 1.0
 */
public class MarketPanel extends JPanel {
	
	/**
	 * Field NINE.
	 * (value is 9)
	 */
	private static final int NINE = 9;
	
	/**
	 * Field FOUR.
	 * (value is 4)
	 */
	private static final int FOUR = 4;
	
	/**
	 * Field TWO.
	 * (value is 2)
	 */
	private static final int TWO = 2;
	
	/**
	 * Field THREE.
	 * (value is 3)
	 */
	private static final int THREE = 3;
	
	/**
	 * field
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * field
	 */
	private final JTextField amountField;

	/**
	 * field
	 */
	private final JButton buyButton;
	
	/**
	 * field
	 */
	private final JButton sellButton;
	
	/**
	 * field
	 */
	private final JTable table;

	/**
	 * field
	 */
	private final Player player;

	/**
	 * Constructor for MarketPanel.
	 * @param player Player
	 */
	public MarketPanel(Player player) {

		this.player = player;

		super.setLayout(new BorderLayout());

		table = new JTable(NINE, FOUR);

        // Make the table non-editable
        table.setModel(new DefaultTableModel(new String[]{
        		"Trade Good", "Price", "Available", "On Ship"}, NINE) {

			@Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        });

        table.setFillsViewportHeight(true);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.setShowGrid(true);
        table.setShowHorizontalLines(true);
        table.setShowVerticalLines(true);

        super.add(table.getTableHeader(), BorderLayout.PAGE_START);
        super.add(table, BorderLayout.CENTER);

        final JPanel buttons = new JPanel();

        final JLabel lblAmount = new JLabel("Amount:");
        amountField = new JTextField();
        buyButton = new JButton("Buy");
        sellButton = new JButton("Sell");

        buttons.add(lblAmount);
        buttons.add(amountField);
        buttons.add(buyButton);
        buttons.add(sellButton);

        super.add(buttons, BorderLayout.SOUTH);

        update();
    }

    /**
     * Method update.
     */
    public final void update() {
        final Marketplace marketplace = player.getLocation().getMarketplace();
        final Ship ship = player.getShip();

        int row = 0;
        for (TradeGood good : marketplace.getTradeGoods()) {
            table.setValueAt(good, row, 0);
            table.setValueAt(marketplace.getQuantity(good), row, 1);
            table.setValueAt(marketplace.getPrice(good), row, TWO);
            table.setValueAt(ship.getTradeGoodQuantity(good), row, THREE);

            row++;
        }
    }

    /**
     * Method getAmount.
    
     * @return int */
    public int getAmount() {
        final String amount = amountField.getText();

        try {
            return Integer.parseInt(amount);
        } catch (Exception e) {
            // Display a warning dialog
			System.err.println(e);

            return -1;
        }
    }

    /**
     * Method getTradeGood.
    
     * @return TradeGood */
    public TradeGood getTradeGood() {
        final int row = table.getSelectedRow();

        try {
            return (TradeGood) table.getValueAt(row, 0);
        } catch (Exception e) {
            System.err.println("Can't convert table content into TradeGood.");
            System.err.println(e.getMessage());
            System.exit(1);

            // to please the compiler
            return null;
        }
    }

    /**
     * Method addActionListener.
     * @param listener ActionListener
     */
    public void addActionListener(ActionListener listener) {
        buyButton.addActionListener(listener);
        sellButton.addActionListener(listener);
    }
}
